﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStringList>
#include <QCompleter>
#include <QStringListModel>
#include <QListWidget>
#include <QKeyEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QCompleter* completer = new QCompleter;
    completer->setFilterMode(Qt::MatchStartsWith);
    completer->setCompletionMode(QCompleter::PopupCompletion);
    QStringList valueList;
    valueList << "564398053@qq.com" << "lijian@163.com" << "959595959@qq.com" << "656565656@qq.com" << "12121212@qq.com" << "plmm@163.com";
    QStringListModel *listModel;
    listModel = new QStringListModel(valueList, this);
    completer->setModel(listModel);

    ui->lineEdit->setCompleter(completer);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter || event->key() == Qt::Key_Down)
     {
       if(ui->lineEdit->text().isEmpty())
       {
         ui->lineEdit->completer()->setCompletionPrefix("");
         ui->lineEdit->completer()->complete();
       }
    }
}

void MainWindow::showComplete()
{
    if(ui->lineEdit->text().isEmpty())
    {
      ui->lineEdit->completer()->setCompletionPrefix("");
      ui->lineEdit->completer()->complete();
    }
}

